package com.kay.utils;

import com.kay.annote.Autowired;
import com.kay.annote.Service;

import java.sql.SQLException;

/**
 * @Description:
 * @Author: Kay
 * @CreateDate: 2021/7/6$ 13:03$
 * @UpdateUser: Kay
 * @UpdateDate: 2021/7/6$ 13:03$
 */
@Service
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;

//    public void setConnectionUtils(ConnectionUtils connectionUtils) {
//        this.connectionUtils = connectionUtils;
//    }

    //    改为由BeanFactory工厂类创建实例对象
//    public TransactionManager() {
//    }
//
//    private static TransactionManager transactionManager = new TransactionManager();
//
//    public static TransactionManager getInstance(){
//        return transactionManager;
//    }

    /**
     * 开启事务
     */
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadCon().setAutoCommit(false);
    }

    /**
     * 提交事务
     * @throws SQLException
     */
    public void commitTransaction() throws SQLException {
        connectionUtils.getCurrentThreadCon().commit();
    }

    /**
     * 回滚事务
     */
    public void rollBack() throws SQLException {
        connectionUtils.getCurrentThreadCon().rollback();
    }
}
